from APITest.online_course_2018.Day3.woniusales import WoniuSales
import random, unittest
from APITest.online_course_2018.Day3.pdbc import exeSql


class WoniusalesTest(unittest.TestCase):
    def setUp(self):
        self.ws = WoniuSales()

    def test_add_customer_success(self):
        name_num = random.randint(0, 9999)
        customname = 'test' + str(name_num)
        phone_num = random.randint(0, 99999999)
        customphone = '181' + str(phone_num)
        childsex = '男'
        childdate = '2018-10-09'
        creditkids = 12
        creditcloth = 20
        print(customname, customphone)
        resp = self.ws.add_customer(customname, customphone, childsex, childdate, creditkids, creditcloth)
        # 断言方法一: 从响应中断言, 优先从响应中断言
        self.assertEqual('add-successful', resp, '会员新增失败')

        # 断言方法二: 从查询接口来进行断言, 如果响应是json串, 可以使用json.loads转换成python对象

        # 断言方法三: 从数据库进行查询并断言
        sql = "SELECT customerphone, customername FROM customer WHERE customerphone = '%s' AND customername= '%s'" % (
        customphone, customname)
        results = exeSql(sql)
        # print(results[0])
        self.assertEqual(customphone, results[0][0])
        self.assertEqual(customname, results[0][1])


if __name__ == '__main__':
    tw = WoniusalesTest()
    tw.add_customer_success()
